package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CencesSdkAdCount;
import com.lebang.domain.TaskRouse;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.CencesSdkAdCountExample;
import com.lebang.model.MemberSdkErrorModel;
import com.lebang.service.MemberSdkBannedService;
import com.lebang.service.NewRouseService;
import com.lebang.service.cences.CencesSdkAdCountService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author ljx
 * @date 2020-12-22
 */
@Controller
@RequestMapping("/sys/cencessdkadcount")
public class CencesSdkAdCountController {
    @Autowired
    private CencesSdkAdCountService baseService;
    @Autowired
    private MemberSdkBannedService memberSdkBannedService;
    @Autowired
    private NewRouseService newRouseService;

    @Login
    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        List<TaskRouse> rouses = newRouseService.selectAll();
        rouses = newRouseService.checkRoot(rouses,session);
        model.addAttribute("item", rouses);
        return "/cencessdkadcount/list";
    }


    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end, Integer status,String sdkKey) {
        CencesSdkAdCountExample example = new CencesSdkAdCountExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        CencesSdkAdCountExample.Criteria criteria = example.createCriteria();
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        if (star != null || end != null) {
            criteria.andCreateTimeBetween(star, end);
        }
        if (!TextUtil.isEmpty(sdkKey)){
            criteria.andSdkKeyEqualTo(sdkKey);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }

    /**
     * 封禁数据源广告
     *
     * @return
     */
    @Login
    @PostMapping("/banned")
    @ResponseBody
    public JsonData banned(String memberId, String sdkKey, String star, Integer reqCount) {
        String cause=star+"请求次数"+reqCount;
        memberSdkBannedService.addRecord(memberId, sdkKey,cause);
        baseService.banned(memberId, sdkKey);
        return Callback.success("success", "操作成功");
    }

    /**
     * 批量封禁数据源广告
     */
    @Login
    @PostMapping("/banall")
    @ResponseBody
    public JsonData bannedAll(String list) {
        List<CencesSdkAdCount> cencesSdkAdCounts = JSON.parseArray(list, CencesSdkAdCount.class);
        for (CencesSdkAdCount cencesSdkAdCount : cencesSdkAdCounts) {
            String cause=cencesSdkAdCount.getCreateTime()+"请求次数"+cencesSdkAdCount.getReqCount();
            memberSdkBannedService.addRecord(cencesSdkAdCount.getMemberId(), cencesSdkAdCount.getSdkKey(),cause);
            baseService.banned(cencesSdkAdCount.getMemberId(), cencesSdkAdCount.getSdkKey());
        }
        return Callback.success("success", "操作成功");
    }

    /**
     * 解封
     */
    @Login
    @PostMapping("/deblocking")
    @ResponseBody
    public JsonData deblocking(String memberId, String sdkKey) {
        memberSdkBannedService.delRecord(memberId, sdkKey);
        baseService.deblocking(memberId, sdkKey);
        return Callback.success("success", "操作成功");
    }

    /**
     * 批量解封
     */
    @Login
    @PostMapping("/deblockingall")
    @ResponseBody
    public JsonData deblockingAll(String list) {
        List<CencesSdkAdCount> cencesSdkAdCounts = JSON.parseArray(list, CencesSdkAdCount.class);
        for (CencesSdkAdCount cencesSdkAdCount : cencesSdkAdCounts) {
            memberSdkBannedService.delRecord(cencesSdkAdCount.getMemberId(), cencesSdkAdCount.getSdkKey());
            baseService.deblocking(cencesSdkAdCount.getMemberId(), cencesSdkAdCount.getSdkKey());
        }
        return Callback.success("success", "操作成功");
    }





    @Login
    @PostMapping("/memberclosememberstatus")
    @ResponseBody
    public JsonData memberCloseMemberstatus(HttpServletRequest request, String memberId,String sdkKey) {
        memberSdkBannedService.addRecord(memberId, sdkKey,"用户广告错误码过多");
        return Callback.success("200", "操作成功");
    }


    @Login
    @PostMapping("/membercancel")
    @ResponseBody
    public JsonData memberCancel(HttpServletRequest request, String memberId,String sdkKey) {
        memberSdkBannedService.delRecord(memberId, sdkKey);
        return Callback.success("200", "操作成功");
    }


    @Login
    @PostMapping("/memberbanall")
    @ResponseBody
    public JsonData memberBanAll(HttpServletRequest request, String list) {
        List<MemberSdkErrorModel> memberSdkErrorModels = JSON.parseArray(list, MemberSdkErrorModel.class);
        memberSdkErrorModels.forEach(s->{
            memberSdkBannedService.addRecord(s.getMemberId(), s.getSdkKey(),"用户广告错误码过多");
        });

        return Callback.success("200", "操作成功");
    }



    @Login
    @PostMapping("/membercancelall")
    @ResponseBody
    public JsonData memebrCancelAll(HttpServletRequest request, String list) {
        List<MemberSdkErrorModel> memberSdkErrorModels = JSON.parseArray(list, MemberSdkErrorModel.class);
        memberSdkErrorModels.forEach(s->{
            memberSdkBannedService.delRecord(s.getMemberId(), s.getSdkKey());
        });

        return Callback.success("200", "操作成功");
    }


}
